home *** CD-ROM | disk | FTP | other *** search
/ PC World Komputer 2010 April / PCWorld0410.iso / pluginy Firefox / 684 / 684.xpi / components / nsIFireFTPUtils.js < prev    next >
Text File  |  2008-03-20  |  7KB  |  210 lines

  1. Components.utils.import("resource://gre/modules/XPCOMUtils.jsm");                               // makes life easier
  2.  
  3. var gThreadManager    = Components.classes["@mozilla.org/thread-manager;1"].getService();       // threading functions
  4. var gMainThread       = gThreadManager.mainThread;
  5.  
  6. function threadedEvent(func) {
  7.   this.run = func;
  8. }
  9.  
  10. threadedEvent.prototype = {
  11.   QueryInterface : XPCOMUtils.generateQI([Components.interfaces.nsIRunnable, Components.interfaces.nsISupports])
  12. };
  13.  
  14. function dispatchEvent(func, isMainThread, isNormal) {
  15.   var target = isMainThread ? gMainThread : gThreadManager.newThread(0);
  16.   target.dispatch(new threadedEvent(func), isNormal ? target.DISPATCH_NORMAL : target.DISPATCH_SYNC);
  17.  
  18.   if (!isMainThread) {
  19.     target.shutdown();
  20.   }
  21. }
  22.  
  23. function FireFTPUtils() { };                                                                    // FireFTPUtils
  24.  
  25. FireFTPUtils.prototype = {
  26.   classDescription  : "FireFTP Utilities",
  27.   classID           : Components.ID("{42bd5782-5c3e-11dc-8314-0800200c9a66}"),
  28.   contractID        : "@nightlight.ws/fireftputils;1",
  29.   _xpcom_categories : [{ category: 'FireFTP Utilities', service: true }],
  30.   QueryInterface    : XPCOMUtils.generateQI([Components.interfaces.nsIFireFTPUtils, Components.interfaces.nsISupports]),
  31.  
  32.   hiddenMode        : false,
  33.  
  34.   getRecursiveFolderData : function(dir, recursiveFolderData) {
  35.     recursiveFolderData = recursiveFolderData.wrappedJSObject.obj;
  36.     var self    = this;
  37.     var func    = function() { self.getRecursiveFolderData2(dir, recursiveFolderData) };        // <strike>separate thread</strike>
  38.     func(); //dispatchEvent(func);
  39.   },
  40.  
  41.   getRecursiveFolderData2 : function(dir, recursiveFolderData) {
  42.     try {
  43.       var entries = dir.directoryEntries;
  44.  
  45.       while (entries.hasMoreElements()) {
  46.         var file = entries.getNext().QueryInterface(Components.interfaces.nsILocalFile);
  47.  
  48.         if (file.exists() && this.testSize(file) && (!file.isHidden() || this.hiddenMode)) {
  49.           if (file.isDirectory()) {
  50.             ++recursiveFolderData.nFolders;
  51.             this.getRecursiveFolderData2(file, recursiveFolderData);
  52.           } else {
  53.             ++recursiveFolderData.nFiles;
  54.           }
  55.  
  56.           recursiveFolderData.nSize += file.fileSize;
  57.         }
  58.       }
  59.     } catch (ex) {
  60.                                                                                                 // do nothing, skip this directory
  61.     }
  62.   },
  63.  
  64.   testSize : function(file) {                                                                   // XXX in linux, files over 2GB throw an exception
  65.     try {
  66.       var x = file.fileSize;
  67.       return true;
  68.     } catch (ex) {
  69.       return false;
  70.     }
  71.   },
  72.  
  73.   generateHash : function(file, hash) {
  74.     var result;
  75.     var self = this;
  76.  
  77.     var func = function() {                                                                     // generate hash, <strike>separate thread</strike>
  78.       try {
  79.         var cryptoHash;
  80.  
  81.         if (hash == 'md5') {
  82.           cryptoHash = Components.interfaces.nsICryptoHash.MD5;
  83.         } else if (hash == 'sha1') {
  84.           cryptoHash = Components.interfaces.nsICryptoHash.SHA1;
  85.         } else if (hash == 'sha256') {
  86.           cryptoHash = Components.interfaces.nsICryptoHash.SHA256;
  87.         } else if (hash == 'sha384') {
  88.           cryptoHash = Components.interfaces.nsICryptoHash.SHA384;
  89.         } else if (hash == 'sha512') {
  90.           cryptoHash = Components.interfaces.nsICryptoHash.SHA512;
  91.         }
  92.  
  93.         var fstream = Components.classes["@mozilla.org/network/file-input-stream;1"].createInstance(Components.interfaces.nsIFileInputStream);
  94.         fstream.init(file, 1, 0, false);
  95.  
  96.         var hashComp = Components.classes["@mozilla.org/security/hash;1"].createInstance(Components.interfaces.nsICryptoHash);
  97.         hashComp.init(cryptoHash);
  98.         hashComp.updateFromStream(fstream, -1);
  99.         result = self.binaryToHex(hashComp.finish(false));
  100.  
  101.         fstream.close();
  102.       } catch (ex) { }
  103.     };
  104.     func(); //dispatchEvent(func);
  105.  
  106.     return result;
  107.   },
  108.  
  109.   binaryToHex : function(input) {                                                               // borrowed from nsUpdateService.js
  110.     var result = "";
  111.  
  112.     for (var i = 0; i < input.length; ++i) {
  113.       var hex = input.charCodeAt(i).toString(16);
  114.  
  115.       if (hex.length == 1) {
  116.         hex = "0" + hex;
  117.       }
  118.  
  119.       result += hex;
  120.     }
  121.  
  122.     return result;
  123.   },
  124.  
  125.   removeFile : function(file) {
  126.     var innerEx = "";
  127.  
  128.     var func = function() {                                                                     // delete file (recursively if dir), <strike>separate thread</strike>
  129.       try {
  130.         file.remove(true);
  131.       } catch (ex) {
  132.         innerEx = ex;
  133.       }
  134.     };
  135.     func(); //dispatchEvent(func);
  136.  
  137.     return innerEx;
  138.   },
  139.  
  140.   extract : function(zip, entry, destFolder) {
  141.     var innerEx = "";
  142.  
  143.     var func = function() {                                                                     // extract file, <strike>separate thread</strike>
  144.       try {
  145.         zip.extract(entry, destFolder);
  146.       } catch (ex) {
  147.         innerEx = ex;
  148.       }
  149.     };
  150.     func(); //dispatchEvent(func);
  151.  
  152.     return innerEx;
  153.   },
  154.  
  155.   cutCopy : function(isCut, file, newFile, newDir, newName) {
  156.     var innerEx = "";
  157.  
  158.     var func = function() {                                                                     // cut or copy file, <strike>separate thread</strike>
  159.       try {
  160.         if (newFile.exists()) {
  161.           newFile.remove(true);
  162.         }
  163.  
  164.         if (isCut) {
  165.           file.moveTo(newDir, newName);                                                         // cut
  166.         } else {
  167.           file.copyTo(newDir, newName);                                                         // or copy
  168.         }
  169.       } catch (ex) {
  170.         innerEx = ex;
  171.       }
  172.     };
  173.     func(); //dispatchEvent(func);
  174.  
  175.     return innerEx;
  176.   },
  177.  
  178.   getFileList : function(dir, files) {
  179.     var innerEx = "";
  180.     var self    = this;
  181.     files       = files.wrappedJSObject.obj;
  182.  
  183.     var func = function() {                                                                     // get file list, <strike>separate thread</strike>
  184.       try {
  185.         var entries = dir.directoryEntries;
  186.  
  187.         while (entries.hasMoreElements()) {
  188.           var file = entries.getNext().QueryInterface(Components.interfaces.nsILocalFile);
  189.  
  190.           if (file.exists() && self.testSize(file) && (!file.isHidden() || self.hiddenMode)) {
  191.             files.push(file);
  192.           }
  193.         }
  194.       } catch (ex) {
  195.         innerEx = ex;
  196.         return;                                                                                 // skip this directory
  197.       }
  198.     };
  199.     func(); //dispatchEvent(func);
  200.  
  201.     return innerEx;
  202.   }
  203. };
  204.  
  205. var components = [FireFTPUtils];                                                                // register components
  206.  
  207. function NSGetModule(compMgr, fileSpec) {
  208.   return XPCOMUtils.generateModule(components);
  209. }
  210.